﻿HmJS.$Import('core.fx');

/*
---
script: Fx.Scroll.Carousel.js
version: 0.9-2
description: Extends Fx.Scroll to work like a carousel
license: MIT-style license.
download: http://mootools.net/forge/p/fx_scroll_carousel
source: http://github.com/rpflorence/Fx.Scroll.Carousel/

authors: Ryan Florence

docs: http://moodocs.net/rpflo/mootools-rpflo/Fx.Scroll.Carousel

requires:
- more/1.2.4.2: [Fx.Scroll]

provides: [Fx.Scroll.Carousel]
...
*/

HmJS.register('base.fx.scroll.carousel', function ($ns) {

	Fx.Scroll.Carousel = new Class({

		Extends: Fx.Scroll,

		options: {
			mode: 'horizontal',
			childSelector: false,
			loopOnScrollEnd: true
		},

		initialize: function (element, options) {
			this.parent(element, options);
			this.cacheElements();
			this.currentIndex = 0;
		},

		cacheElements: function () {
			var cs = this.options.childSelector;
			if (cs) {
				els = this.element.getElements(cs);
			} else if (this.options.mode == 'horizontal') {
				els = this.element.getElements(':first-child > *');
			} else {
				els = this.element.getChildren();
			}
			this.elements = els;
			return this;
		},

		toIndex: function (index) {
			if (this.checkLink() || index >= this.elements.length) {
				return this;
			}
			this.currentIndex = index;
			this.toElement(this.elements[this.currentIndex]);
			return this;
		},

		toFirst: function () {
			return this.toIndex(0);
		},

		toLast: function () {
			return this.toIndex(this.elements.length - 1);
		},

		toNext: function () {
			if (this.checkLink()) { return this; }
			this.toIndex(this.getNextIndex());
			this.fireEvent('next');
			return this;
		},

		toPrevious: function () {
			if (this.checkLink()) { return this; }
			this.toIndex(this.getPreviousIndex());
			this.fireEvent('previous');
			return this;
		},

		getNextIndex: function () {
			this.currentIndex++;
			if (this.currentIndex == this.elements.length || this.checkScroll()) {
				this.fireEvent('loop');
				this.fireEvent('nextLoop');
				return 0;
			} else {
				return this.currentIndex;
			};
		},

		getPreviousIndex: function () {
			this.currentIndex--;
			var check = this.checkScroll();
			if (this.currentIndex < 0 || check) {
				this.fireEvent('loop');
				this.fireEvent('previousLoop');
				return (check) ? this.getOffsetIndex() : this.elements.length - 1;
			} else {
				return this.currentIndex;
			}
		},

		getOffsetIndex: function () {
			var el = this.element,
					el0 = this.elements[0];
			var visible = (this.options.mode == 'horizontal') ?
					el.getStyle('width').toInt() / el0.getStyle('width').toInt() :
					el.getStyle('height').toInt() / el0.getStyle('height').toInt();
			return this.currentIndex + 1 - visible;
		},

		checkLink: function () {
			return (this.timer && this.options.link == 'ignore');
		},

		checkScroll: function () {
			if (!this.options.loopOnScrollEnd) {
				return false;
			}
			var scroll, total,
					el = this.element;
			if (this.options.mode == 'horizontal') {
				scroll = el.getScroll().x;
				total = el.getScrollSize().x - el.getSize().x;
			} else {
				scroll = el.getScroll().y;
				total = el.getScrollSize().y - el.getSize().y;
			}
			return (scroll == total);
		},

		getCurrent: function () {
			return this.elements[this.currentIndex];
		}

	});

});